#include "gtest/gtest.h"
#include "IContext.h"

USING_NS_B2D

class _ITestInterface { };
class _TestClass : public _ITestInterface { };
class _ITestInterface2 { };
class _TestClass2a : public _ITestInterface2 { };
class _TestClass2b : public _ITestInterface2 { };

TEST(test_context, test_register)
{
	DefaultContext context;
	_TestClass myClass;
	_TestClass2a myClass2;

	bool beforeIsNull = !GetContext().getInstance<_ITestInterface>()
		&& !GetContext().getInstance<_ITestInterface2>();

	GetContext_RW().registerInstance<_ITestInterface>(myClass);
	GetContext_RW().registerInstance<_ITestInterface2>(myClass2);

	bool afterCorrect = GetContext().getInstance<_ITestInterface>() == &myClass
		&& GetContext().getInstance<_ITestInterface2>() == &myClass2;

	
	EXPECT_EQ(true, beforeIsNull);
	EXPECT_EQ(true, afterCorrect);
}

TEST(test_context, test_unregister)
{
	DefaultContext context;
	_TestClass myClass;

	GetContext_RW().registerInstance<_ITestInterface>(myClass);
	bool registered = GetContext().getInstance<_ITestInterface>() == &myClass;

	GetContext_RW().unregisterInstance<_ITestInterface>();
	bool unregistered = !GetContext().getInstance<_ITestInterface>();
	
	EXPECT_EQ(true, registered);
	EXPECT_EQ(true, unregistered);
}

TEST(test_context, test_replacing)
{
	DefaultContext context;
	_TestClass2a myClassA;
	_TestClass2b myClassB;

	bool isNull = !GetContext().getInstance<_ITestInterface2>();

	GetContext_RW().registerInstance<_ITestInterface2>(myClassA);
	bool isA = GetContext().getInstance<_ITestInterface2>() == &myClassA;
	GetContext_RW().registerInstance<_ITestInterface2>(myClassB);
	bool isB = GetContext().getInstance<_ITestInterface2>() == &myClassB;

	
	EXPECT_EQ(true, isNull);
	EXPECT_EQ(true, isA);
	EXPECT_EQ(true, isB);
}